package com.naodong.shenluntiku.mvp.view.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.di.component.DaggerStudySetComponent;
import com.naodong.shenluntiku.di.module.StudySetModule;
import com.naodong.shenluntiku.mvp.contract.StudySetContract;
import com.naodong.shenluntiku.mvp.model.bean.EventBusTag;
import com.naodong.shenluntiku.mvp.model.bean.IdAndChosen;
import com.naodong.shenluntiku.mvp.model.bean.StudySet;
import com.naodong.shenluntiku.mvp.presenter.StudySetPresenter;

import butterknife.BindView;
import me.shingohu.man.base.BaseMVPActivity;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.intergation.EventManager;

/**
 * Created by Administrator on 2017/5/29.
 */

public class StudySetUpActivity extends BaseMVPActivity<StudySetPresenter> implements StudySetContract.View {
    @BindView(R.id.difficultyTabLayout)
    TabLayout difficultyTabLayout;
    @BindView(R.id.quickSW)
    SwitchCompat quickSW;
    @BindView(R.id.errorParseSW)
    SwitchCompat errorParseSW;
    @BindView(R.id.subjectTypeView)
    LinearLayout subjectTypeView;
    @BindView(R.id.videoParseSW)
    SwitchCompat videoParseSW;
    @BindView(R.id.difficultyTV)
    TextView difficultyTV;
    @BindView(R.id.quickTV)
    TextView quickTV;
    @BindView(R.id.errorParseTV)
    TextView errorParseTV;
    @BindView(R.id.videoParseTV)
    TextView videoParseTV;
    @BindView(R.id.mainView)
    ScrollView mainView;

    StudySet studySet;

    String originalStudySetString;

    @Override
    protected void onCreateInit(Bundle savedInstanceState) {
        initView();
    }

    private void initView() {
        mPresenter.requestStudySet();
    }

    private void setDifficultyTabLayout() {
        difficultyTabLayout.removeAllTabs();
        difficultyTabLayout.clearOnTabSelectedListeners();
        for (IdAndChosen idAndChosen : studySet.getDifficulty().getValueList()) {
            TabLayout.Tab tabItem = difficultyTabLayout.newTab();
            tabItem.setTag(idAndChosen.getId());
            tabItem.setText(idAndChosen.getText());
            difficultyTabLayout.addTab(tabItem, idAndChosen.getIsChosen() == 1);
        }
        LinearLayout linearLayout = (LinearLayout) difficultyTabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
                R.drawable.layout_divider_vertical));

        difficultyTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mPresenter.requestUpdateStudySet(studySet.getDifficulty().getType(), 1, tab.getTag() + "");
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerStudySetComponent.builder().appComponent(appComponent).studySetModule(new StudySetModule(this)).build().inject(this);
    }

    @Override
    protected int layoutId() {
        return R.layout.a_study_set;
    }


    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void onRequesrStart() {
        progressHudUtil.showProgressHud();
    }

    @Override
    public void onRequestFinish() {
        progressHudUtil.dismissProgressHud();
    }

    @Override
    public void onStudySet(StudySet studySet) {
        originalStudySetString = new Gson().toJson(studySet);
        setData(studySet);
    }

    @Override
    public void onUpdateStudySet(String type, int isChosen, String value) {
        if (type.equals(studySet.getDifficulty().getType())) {
            //设置的是难度
            for (IdAndChosen idAndChosen : studySet.getDifficulty().getValueList()) {
                idAndChosen.setIsChosen(0);
                if (idAndChosen.getId() == Integer.valueOf(value)) {
                    idAndChosen.setIsChosen(1);
                }
            }
        } else if (type.equals(studySet.getChoiceSubject().getType())) {
            //设置快速练习
            studySet.getChoiceSubject().setIsChosen(isChosen);
        } else if (type.equals(studySet.getArticle().getType())) {
            //真题解析
            studySet.getArticle().setIsChosen(isChosen);
            //修改为选中后全部设置为选中
            for (IdAndChosen idAndChosen : studySet.getArticle().getSonValueList()) {
                idAndChosen.setIsChosen(isChosen);
            }

        } else if (type.equals(studySet.getVideo().getType())) {

            //视频解析
            studySet.getVideo().setIsChosen(isChosen);
        } else {
            for (IdAndChosen idAndChosen : studySet.getArticle().getSonValueList()) {
                if (type.equals(idAndChosen.getType())) {
                    idAndChosen.setIsChosen(isChosen);
                    break;
                }
            }
        }
        setData(studySet);
    }

    @Override
    public void onUpdateStudyFail() {
        setData(studySet);
    }

    private void setData(StudySet studySet) {

        this.studySet = studySet;


        mainView.setVisibility(View.VISIBLE);


        errorParseSW.setOnCheckedChangeListener(null);
        quickSW.setOnCheckedChangeListener(null);
        videoParseSW.setOnCheckedChangeListener(null);

        difficultyTV.setText(studySet.getDifficulty().getText());
        quickTV.setText(studySet.getChoiceSubject().getText());
        quickSW.setChecked(studySet.getChoiceSubject().getIsChosen() == 1);
        errorParseTV.setText(studySet.getArticle().getText());
        errorParseSW.setChecked(studySet.getArticle().getIsChosen() == 1);
        videoParseTV.setText(studySet.getVideo().getText());
        videoParseSW.setChecked(studySet.getVideo().getIsChosen() == 1);
        if (errorParseSW.isChecked()) {
            subjectTypeView.setVisibility(View.VISIBLE);
        } else {
            subjectTypeView.setVisibility(View.GONE);
        }
        setDifficultyTabLayout();
        setSubjectTypeView();

        errorParseSW.setOnCheckedChangeListener((buttonView, isChecked) -> mPresenter.requestUpdateStudySet(studySet.getArticle().getType(), isChecked ? 1 : 0, null));
        videoParseSW.setOnCheckedChangeListener((buttonView, isChecked) -> mPresenter.requestUpdateStudySet(studySet.getVideo().getType(), isChecked ? 1 : 0, null));
        quickSW.setOnCheckedChangeListener((buttonView, isChecked) -> mPresenter.requestUpdateStudySet(studySet.getChoiceSubject().getType(), isChecked ? 1 : 0, null));
    }


    private void setSubjectTypeView() {

        LayoutInflater inflater = LayoutInflater.from(mActivity);
        subjectTypeView.removeAllViews();
        for (IdAndChosen idAndChosen : studySet.getArticle().getSonValueList()) {
            View view = inflater.inflate(R.layout.study_set_subject_type_item, null);
            TextView tv = (TextView) view.findViewById(R.id.tv);
            ImageView iv = (ImageView) view.findViewById(R.id.checkIV);
            tv.setText(idAndChosen.getText());
            iv.setSelected(idAndChosen.getIsChosen() == 1);
            subjectTypeView.addView(view);

            view.setOnClickListener(v -> {
                mPresenter.requestUpdateStudySet(idAndChosen.getType(), idAndChosen.getIsChosen() == 1 ? 0 : 1, null);
            });
        }
    }

    @Override
    public void finish() {
        checkChange();
        super.finish();
    }

    private void checkChange() {
        if (originalStudySetString != null) {
            if (!originalStudySetString.equals(new Gson().toJson(studySet))) {
                EventManager.getInstance().postEvent(EventBusTag.NOTIFY_STUDYFRAGMENT_REFRESH);
            }
        }
    }
}
